add ObjectDetails-struct and use it in list_objects-function
authorManuel Stühn <freebsd@justmail.de>
Tue, 16 Nov 2021 11:10:50 +0000 (12:10 +0100)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:57 +0000 (12:53 -0400)
rust-bindings/rust/src/lib.rs
rust-bindings/rust/src/object_details.rs [new file with mode: 0644]
rust-bindings/rust/src/repo.rs

index 5bce2adb64f2d72f819314d6669236b13311b1e4..ae2debb523f1ac8027b23649465950dfe3c6b1c5 100644 (file)
@@ -48,6 +48,8 @@ mod kernel_args;
 pub use crate::kernel_args::*;
 mod object_name;
 pub use crate::object_name::*;
+mod object_details;
+pub use crate::object_details::*;
 mod repo;
 pub use crate::repo::*;
 #[cfg(any(feature = "v2016_8", feature = "dox"))]
diff --git a/rust-bindings/rust/src/object_details.rs b/rust-bindings/rust/src/object_details.rs
new file mode 100644 (file)
index 0000000..12a580c
--- /dev/null
@@ -0,0 +1,48 @@
+use glib;
+use std::fmt::Display;
+use std::fmt::Formatter;
+use std::fmt::Error;
+
+/// Details of an object in an OSTree repo. It contains information about if
+/// the object is "loose", and contains a list of pack file checksums in which
+/// this object appears.
+#[derive(Debug)]
+pub struct ObjectDetails {
+    loose: bool,
+    object_appearances: Vec<String>,
+}
+
+impl ObjectDetails {
+    /// Create a new `ObjectDetails` from a serialized representation.
+    pub fn new_from_variant(variant: glib::Variant) -> Option<ObjectDetails> {
+        let deserialize = variant.get::<(bool, Vec<String>)>()?;
+        Some(ObjectDetails {
+            loose: deserialize.0,
+            object_appearances: deserialize.1,
+        })
+    }
+
+    /// is object available "loose"
+    pub fn is_loose(&self) -> bool {
+        self.loose
+    }
+
+    /// Provide list of pack file checksums in which the object appears
+    pub fn appearances(&self) -> &Vec<String> {
+        &self.object_appearances
+    }
+
+    /// Format this `ObjectDetails` as a string.
+    fn to_string(&self) -> String {
+        format!("Object is {} loose and appears in {} checksums",
+                if self.loose {"available"} else {"not available"},
+                self.object_appearances.len() )
+    }
+}
+
+impl Display for ObjectDetails{
+    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
+        write!(f, "{}", self.to_string())
+    }
+}
+
index f44a6c2d57ea5be5da6c0cb225cba72e2728e6dc..edec8dc5e0ebf2492cabd0c14e004a338442b0ce 100644 (file)
@@ -1,6 +1,6 @@
 #[cfg(any(feature = "v2016_4", feature = "dox"))]
 use crate::RepoListRefsExtFlags;
-use crate::{Checksum, ObjectName, ObjectType, Repo, RepoTransactionStats};
+use crate::{Checksum, ObjectName, ObjectDetails, ObjectType, Repo, RepoTransactionStats};
 use ffi;
 use ffi::OstreeRepoListObjectsFlags;
 use glib::ffi as glib_sys;
@@ -31,9 +31,9 @@ unsafe extern "C" fn read_variant_object_map(
 ) {
     let key: glib::Variant = from_glib_none(key as *const glib_sys::GVariant);
     let value: glib::Variant = from_glib_none(value as *const glib_sys::GVariant);
-    if let Some(insert) = value.get::<(bool, Vec<String>)>() {
-        let set: &mut HashMap<ObjectName, (bool, Vec<String>)> = &mut *(hash_set as *mut HashMap<ObjectName, (bool, Vec<String>)>);
-        set.insert(ObjectName::new_from_variant(key), insert);
+    let set: &mut HashMap<ObjectName, ObjectDetails> = &mut *(hash_set as *mut HashMap<ObjectName, ObjectDetails>);
+    if let Some(details) = ObjectDetails::new_from_variant(value) {
+        set.insert(ObjectName::new_from_variant(key), details);
     }
 }
 
@@ -48,12 +48,12 @@ unsafe fn from_glib_container_variant_set(ptr: *mut glib_sys::GHashTable) -> Has
     set
 }
 
-unsafe fn from_glib_container_variant_map(ptr: *mut glib_sys::GHashTable) -> HashMap<ObjectName, (bool, Vec<String>)> {
+unsafe fn from_glib_container_variant_map(ptr: *mut glib_sys::GHashTable) -> HashMap<ObjectName, ObjectDetails> {
     let mut set = HashMap::new();
     glib_sys::g_hash_table_foreach(
         ptr,
         Some(read_variant_object_map),
-        &mut set as *mut HashMap<ObjectName, (bool, Vec<String>)> as *mut _,
+        &mut set as *mut HashMap<ObjectName, ObjectDetails> as *mut _,
     );
     glib_sys::g_hash_table_unref(ptr);
     set
@@ -177,7 +177,7 @@ impl Repo {
         &self,
         flags: OstreeRepoListObjectsFlags,
         cancellable: Option<&P>,
-    ) -> Result<HashMap<ObjectName, (bool, Vec<String>)>, Error> {
+    ) -> Result<HashMap<ObjectName, ObjectDetails>, Error> {
         unsafe {
             let mut error = ptr::null_mut();
             let mut hashtable = ptr::null_mut();